package com.cssw.fyzb.web.controller;

import com.cssw.fyzb.base.web.context.UserContext;
import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.web.dto.DeleteSysMsgDto;
import com.cssw.fyzb.web.dto.SaveMsgDto;
import com.cssw.fyzb.web.dto.UpdateMsgDto;
import com.cssw.fyzb.web.service.SystemMsgService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Auther: zzf
 * @Date: 2020/12/4 16:33
 * @Description:
 */
@ApiModel("系统消息接口")
@RestController
@RequestMapping("/systemMsg")
public class SystemMsgController {

    @Autowired
    private SystemMsgService systemMsgService;


    @ApiOperation(value = "消息列表", response = AjaxPage.class)
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "content", value = "消息内容", paramType = "query", dataType = "string", required = false),
            @ApiImplicitParam(name = "title", value = "消息标题", paramType = "query", dataType = "string", required = false),
    })
    public AjaxPage list(int page, int limit, String content, String title) {
        Map param = new HashMap();
        param.put("content", content);
        param.put("title", title);
        return systemMsgService.list(page, limit, param);
    }

    @ApiOperation(value = "消息详情", response = AjaxPage.class)
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public AjaxData detail(Long msgId) {
        return systemMsgService.detail(msgId);
    }

    @ApiOperation(value = "新增系统消息", response = AjaxPage.class)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxData save(@RequestBody SaveMsgDto dto) {
        return systemMsgService.save(dto.getTitle(), dto.getContent(), UserContext.getUserId());
    }

    @ApiOperation(value = "修改系统消息", response = AjaxPage.class)
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxData update(@RequestBody UpdateMsgDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return systemMsgService.update(dto.getMsgId(), dto.getTitle(), dto.getContent(), UserContext.getUserId());
    }

    @ApiOperation(value = "删除系统消息", response = AjaxPage.class)
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxData delete(@RequestBody DeleteSysMsgDto dto, BindingResult result) {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError fieldError = errors.get(0);
            return AjaxData.failure(fieldError.getDefaultMessage());
        }
        return systemMsgService.delete(dto.getMsgId(), UserContext.getUserId());
    }
}
